
package cn.javaxc.mvcwork.framework.servlets.url.base;
import cn.javaxc.mvcwork.controller.MVCConstant;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;





public class URLMap extends TFormBean {

  private static final long serialVersionUID = -5854362050325558717L;

  private int id = -1;

  private long timeInMillis = -1;

  private String url = null;

  
  public int getId() {
    return id;
  }

  
  public void setId(int id) {
    this.id = id;
  }

  
  public void setId(String tmp) {
    this.id = Integer.parseInt(tmp);
  }

  
  public long getTimeInMillis() {
    return timeInMillis;
  }

  
  public void setTimeInMillis(long timeInMillis) {
    this.timeInMillis = timeInMillis;
  }

  
  public void setTimeInMillis(String tmp) {
    this.timeInMillis = Long.parseLong(tmp);
  }

  
  public String getUrl() {
    return url;
  }

  
  public void setUrl(String url) {
    this.url = url;
  }

  
  public URLMap() {
  }

  
  public URLMap(Connection db, int id) throws SQLException {
    queryRecord(db, id);
  }

  
  public URLMap(ResultSet rs) throws SQLException {
    buildRecord(rs);
  }

  
  public void queryRecord(Connection db, int id) throws SQLException {
    if (id == -1) {
      throw new SQLException("Invalid URL MAP Number");
    }
    try {
      PreparedStatement pst = db.prepareStatement("SELECT urlmp.* "
          + "FROM url_map urlmp " + "WHERE urlmp.url_id = ? ");
      pst.setInt(1, id);
      ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        buildRecord(rs);
      }
      rs.close();
      pst.close();
    } catch (Exception e) {
      // TODO: handle exception
    }
    if (this.getId() == -1) {
      throw new SQLException(MVCConstant.NOT_FOUND_ERROR);
    }
  }

  
  private void buildRecord(ResultSet rs) throws SQLException {
    // url_map table
    this.setId(rs.getInt("url_id"));
    this.setTimeInMillis(rs.getLong("time_in_millis"));
    this.setUrl(rs.getString("url"));
  }

  
  public boolean insert(Connection db) throws SQLException {
    boolean result = false;
    boolean commit = false;
    try {
      commit = db.getAutoCommit();
      if (commit) {
        db.setAutoCommit(false);
      }
      StringBuffer sql = new StringBuffer();
      sql.append(" INSERT INTO url_map (");
      if (id > -1) {
        sql.append(" url_id,");
      }
      sql.append(" url, time_in_millis )");

      sql.append("VALUES(");
      if (id > -1) {
        sql.append(" ?,");
      }
      sql.append(" ?, ? )");

      int i = 0;
      PreparedStatement pst = db.prepareStatement(sql.toString());
      if (id > -1) {
        pst.setInt(++i, this.getId());
      }
      pst.setString(++i, this.getUrl());
      if (timeInMillis == -1) {
        timeInMillis = System.currentTimeMillis();
      }
      pst.setLong(++i, this.getTimeInMillis());

      pst.execute();
      pst.close();
      id = DatabaseUtils.getCurrVal(db, "url_map_url_id_seq", id);
      if (commit) {
        db.commit();
      }
      result = true;
    } catch (SQLException e) {
      if (commit) {
        db.rollback();
      }
      throw new SQLException(e.getMessage());
    } finally {
      if (commit) {
        db.setAutoCommit(true);
      }
    }
    return result;
  }

  
  public String getURLAlias() {
    return "/url" + "/" + (new Long(this.timeInMillis)).toString() + "/"
        + (new Integer(this.id)).toString();
  }
}
